package com.kzd.gtboon.main

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.AppUtils
import com.kzd.gtboon.AgreeActivity
import com.kzd.gtboon.MainActivity
import com.kzd.gtboon.entity.*
import com.kzd.gtboon.main.game.*
import com.kzd.gtboon.main.home.*
import com.kzd.gtboon.main.mine.*
import com.kzd.gtboon.main.service.ProblemDetailsActivity
import com.kzd.gtboon.main.service.ServiceMessageActivity

const val AUTH_KEY_UID = "uid"
const val AUTH_REQUEST_CODE = 1
const val AUTH_RESULT_CODE = 100
const val AGREE_REQUEST_CODE = 2

fun toActivity(page: String, isLogin: Int = 0) {
    val uri = Uri.parse(page)
    val path = uri.path
        ?: return
    val bundle = Bundle().apply {
        (uri.encodedQuery?.split(("&")) ?: return@apply).forEach {
            val keyValue = it.split(("=")).toTypedArray()
            if (keyValue.size == 2) {
                putString(keyValue[0], keyValue[1])
            }
        }
    }
    if (isLogin == 0) {
        ActivityUtils.startActivity(bundle, AppUtils.getAppPackageName(), path)
    } else {
        checkLogin(path)
    }
}

/**
 * 跳转-协议页面
 * */
fun toAgreeActivity(activity: Activity? = null) {
    if (activity == null) {
        ActivityUtils.startActivity(AgreeActivity::class.java)
    } else {
        ActivityUtils.startActivityForResult(
            activity,
            AgreeActivity::class.java,
            AGREE_REQUEST_CODE
        )
    }
}

const val KEY_MAIN_INDEX = "KEY_MAIN_INDEX"

/**
 * 跳转-主页面 com.kzd.gtboon.MainActivity
 *
 * @param index 0=首页1=游戏2=赚金3客服4=我的
 * */
fun toMainActivity(index: Int = 0) {
    val bundle = Bundle().apply {
        putString(KEY_MAIN_INDEX, index.toString())
    }
    ActivityUtils.startActivity(bundle, MainActivity::class.java)
}

/**
 * 跳转-领券中心 com.kzd.gtboon.main.home.HomeCouponActivity
 * */
fun toHomeCouponActivity() {
    ActivityUtils.startActivity(HomeCouponActivity::class.java)
}

const val KEY_DOINGS_TITLE = "KEY_DOINGS_TITLE"
const val KEY_DOINGS_TYPE = "KEY_DOINGS_TYPE"
const val KEY_DOINGS_DOING = "KEY_DOINGS_DOING"

/**
 * 跳转-活动页面 com.kzd.gtboon.main.home.HomeDoingsActivity
 *
 * @param title 标题
 * @param type 活动类型 'GMList' = '免费当GM', 'changwangList'='0元畅玩', 'chongzhiList' ='免费领充值', 'jingXuanList' ='精选IP大作',
 * */
fun toHomeDoingsActivity(title: String, type: String, doing: Doings<GameDoing>? = null) {
    val bundle = Bundle().apply {
        putString(KEY_DOINGS_TITLE, title)
        putString(KEY_DOINGS_TYPE, type)
        putSerializable(KEY_DOINGS_DOING, doing)
    }
    ActivityUtils.startActivity(bundle, HomeDoingsActivity::class.java)
}

/**
 * 跳转-游戏礼包 com.kzd.gtboon.main.home.HomePresentActivity
 * */
fun toHomePresentActivity() {
    ActivityUtils.startActivity(HomePresentActivity::class.java)
}

/**
 * 跳转-搜索页面 com.kzd.gtboon.main.home.HomeSearchActivity
 * */
fun toHomeSearchActivity() {
    ActivityUtils.startActivity(HomeSearchActivity::class.java)
}

/**
 * 跳转-开服表  com.kzd.gtboon.main.home.HomeServiceActivity
 * */
fun toHomeServiceActivity() {
    ActivityUtils.startActivity(HomeServiceActivity::class.java)
}

const val KEY_HOME_TRAVEL_INDEX = "KEY_HOME_TRAVEL_INDEX"

/**
 * 跳转-新游首发 com.kzd.gtboon.main.home.HomeTravelActivity
 * */
fun toHomeTravelActivity(index: Int = 0) {
    val bundle = Bundle().apply {
        putString(KEY_HOME_TRAVEL_INDEX, index.toString())
    }
    ActivityUtils.startActivity(bundle, HomeTravelActivity::class.java)
}


const val KEY_GAME_TITLE = "KEY_GAME_TITLE"
const val KEY_GAME_ICON = "KEY_GAME_ICON"
const val KEY_GAME_ID = "KEY_GAME_ID"

fun toGameH5Activity(title: String? = null, icon: String? = null, id: String? = null) {
    val bundle = Bundle().apply {
        putString(KEY_GAME_TITLE, title)
        putString(KEY_GAME_ICON, icon)
        putString(KEY_GAME_ID, id)
    }
    checkLogin(bundle, GameH5Activity::class.java)
}

const val KEY_GAME_DETAILS_ID = "KEY_GAME_DETAILS_ID"
const val KEY_GAME_DETAILS_INDEX = "KEY_GAME_DETAILS_INDEX"

/**
 * 跳转-游戏详情页 com.kzd.gtboon.main.game.GameDetailsActivity
 *
 * @param gid 游戏id
 * @param index 0=详情 1=福利 2=开服 3评论 4=交易
 * */
fun toGameDetailsActivity(gid: String, index: Int = 0) {
    val bundle = Bundle().apply {
        putString(KEY_GAME_DETAILS_ID, gid)
        putString(KEY_GAME_DETAILS_INDEX, index.toString())
    }
    ActivityUtils.startActivity(bundle, GameDetailsActivity::class.java)
}

const val KEY_GAME_DETAILS_TITLE = "KEY_GAME_DETAILS_TITLE"
const val KEY_GAME_DETAILS_VIP = "KEY_GAME_DETAILS_VIP"
const val KEY_GAME_DETAILS_VIP_STR = "KEY_GAME_DETAILS_VIP_STR"

/**
 * 跳转-游戏详情VIP com.kzd.gtboon.main.game.GameVipActivity
 *
 * @param title 游戏名称
 * @param gameVip 游戏vip列表
 * */
fun toGameVipActivity(title: String, gameVip: String, vipStr: String?) {
    val bundle = Bundle().apply {
        putString(KEY_GAME_DETAILS_TITLE, title)
        putString(KEY_GAME_DETAILS_VIP, gameVip)
        putString(KEY_GAME_DETAILS_VIP_STR, vipStr)
    }
    ActivityUtils.startActivity(bundle, GameVipActivity::class.java)
}

/**
 * 跳转-更多开服 com.kzd.gtboon.main.game.ServiceMoreActivity
 *
 * @param title 游戏名称
 * @param gid 游戏id
 * */
fun toServiceMoreActivity(title: String, gid: String) {
    val bundle = Bundle().apply {
        putString(KEY_GAME_DETAILS_TITLE, title)
        putString(KEY_GAME_DETAILS_ID, gid)
    }
    ActivityUtils.startActivity(bundle, ServiceMoreActivity::class.java)
}

/**
 * 跳转-代金券 com.kzd.gtboon.main.mine.MineCouponActivity
 * */
fun toMineCouponActivity() {
    checkLogin(MineCouponActivity::class.java)
}

/**
 * 跳转-我的礼包 com.kzd.gtboon.main.mine.MineGiftActivity
 * */
fun toMineGiftActivity() {
    checkLogin(MineGiftActivity::class.java)
}


/**
 * 跳转-个人中心 com.kzd.gtboon.main.mine.MineUserActivity
 * */
fun toMineUserActivity() {
    checkLogin(MineUserActivity::class.java)
}

/**
 * 跳转-问题详情 com.kzd.gtboon.main.service.ProblemDetailsActivity
 * */
const val KEY_PROBLEM_ACTION = "KEY_PROBLEM_ACTION"
fun toProblemDetailsActivity(action: ProblemDetailsAction) {
    val bundle = Bundle().apply {
        putSerializable(KEY_PROBLEM_ACTION, action)
    }
    ActivityUtils.startActivity(bundle, ProblemDetailsActivity::class.java)
}

fun toServiceMessageActivity() {
    ActivityUtils.startActivity(ServiceMessageActivity::class.java)
}

const val KEY_RICH_TEXT_TITLE = "KEY_RICH_TEXT_TITLE"
const val KEY_RICH_TEXT_URL = "KEY_RICH_TEXT_URL"
const val KEY_RICH_TEXT_CONTENT = "KEY_RICH_TEXT_CONTENT"
const val KEY_RICH_TEXT_TYPE = "KEY_RICH_TEXT_TYPE"

fun toMineRichTextActivityCon(title: String? = null, content: String?) {
    val bundle = Bundle().apply {
        putString(KEY_RICH_TEXT_TITLE, title)
        putString(KEY_RICH_TEXT_CONTENT, content)
    }
    ActivityUtils.startActivity(bundle, MineRichTextActivity::class.java)
}

fun toMineRichTextActivityApi(title: String? = null, type: String?) {
    val bundle = Bundle().apply {
        putString(KEY_RICH_TEXT_TITLE, title)
        putString(KEY_RICH_TEXT_TYPE, type)
    }
    ActivityUtils.startActivity(bundle, MineRichTextActivity::class.java)
}

/**
 * 跳转-外部应用QQ
 * */
fun toExternalAppQQ(activity: Activity) {
    //val url = "mqqwpa://im/chat?chat_type=wpa&uin=2850227014"
    val url = "https://wpa1.qq.com/CnFgdQWE?_type=wpa&qidian=true"
    activity.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
}